/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.login;

import com.aeky.client.common.AppParams;
import com.aeky.client.common.DefaultCallback;
import com.aeky.client.rpcservice.LoginRPCService;
import com.aeky.client.rpcservice.LoginRPCServiceAsync;
import com.aeky.model.User;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

/**
 * 用于用户登录的clickHandler
 * 
 * @author aeky
 */
public abstract class LoginClickHandler implements ClickHandler {

    private final LoginRPCServiceAsync service = AppParams.getService(LoginRPCService.class);

    /**
     * 获得用户名
     * 
     * @return String
     */
    abstract String getLoginName();

    /**
     * 获得密码
     * 
     * @return String
     */
    abstract String getPassword();

    /*
     * 
     * @see
     * com.smartgwt.client.widgets.events.ClickHandler#onClick(com.smartgwt.
     * client.widgets.events.ClickEvent)
     */
    public void onClick(ClickEvent event) {
        User record = new User();
        record.setLoginName(getLoginName());
        record.setPassword(getPassword());
        AppParams.getInstance().showProgressBar();
        service.login(record, new DefaultCallback<User>() {
            public void onSuccess(User user) {
                AppParams.getInstance().hideProgressBar();
                AppParams.getInstance().setUser(user);
                AppParams.getInstance().showMain();
            }
        });
    }

}
